﻿namespace Api.Controllers;

/// <summary>
/// 系统设置
/// </summary>
/// <param name="systemConfigBLL"></param>
/// <param name="hostingEnv"></param>
/// <param name="operationLogBLL"></param>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class SystemConfigController(ISystemConfigBLL systemConfigBLL, IWebHostEnvironment hostingEnv, IOperationLogBLL operationLogBLL) : BaseController(operationLogBLL)
{
    private readonly ISystemConfigBLL _systemConfigBLL = systemConfigBLL;
    private readonly IWebHostEnvironment _hostingEnv = hostingEnv;

    #region 获取所有系统设置
    /// <summary>
    /// 获取所有系统设置
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ResultModel<List<SystemConfig>>> GetAllSystemConfigAsync()
    {
        RefAsync<int> total = 0;
        var list = await _systemConfigBLL.GetAllAsync();
        var ret = new ResultModel<List<SystemConfig>>
        {
            data = list,
            count = total,
        };
        return ret;
    }
    #endregion

    #region 获取系统名称和logo
    /// <summary>
    /// 获取系统名称和logo
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [AllowAnonymous]
    public async Task<ResultModel<List<SystemConfig>>> GetLogoAndSystemNameAsync()
    {
        RefAsync<int> total = 0;
        var list = await _systemConfigBLL.GetAllAsync();
        list = list.Where(x => x.Key == "system_name" || x.Key == "logo_url").ToList();
        var ret = new ResultModel<List<SystemConfig>>
        {
            data = list,
            count = list != null ? list.Count : 0,
        };
        return ret;
    }
    #endregion

    #region 上传系统logo
    /// <summary>
    /// 上传系统logo
    /// </summary>
    /// <param name="formFile">必填</param>
    /// <returns></returns>
    [HttpPost]
    public ResultModel UploadImg(IFormFile formFile)
    {
        return OperationExecuteCustom(new Func<ResultModel>(() =>
        {
            ResultModel resultModel = new ResultModel();
            if (formFile == null)
            {
                formFile = Request.Form.Files[0];
            }
            string subPath = Path.Combine("logo");
            string basePath = _hostingEnv.WebRootPath;
            FileUploadHelper fileUploadHelper = new FileUploadHelper(formFile, basePath, subPath, "Pic");
            if (!fileUploadHelper.IsPicture())
            {
                resultModel.code = -1;
                resultModel.msg = "图片格式错误";
                return resultModel;
            }
            string url = fileUploadHelper.Upload();
            resultModel.code = 1;
            resultModel.data = url;
            return resultModel;
        }), new AddOperationLogInput { Content = $"上传系统logo" });
    }
    #endregion

    #region 保存系统参数配置
    /// <summary>
    /// 保存系统参数配置
    /// </summary>
    /// <param name="configList">ConfigCode(系统名称：system_name，系统logo：logo_url，是否自动同步课表(0:否,1:是)：autosyn_curr)</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ResultModel> SaveSystemConfigAsync([FromBody] List<SaveSystemConfigRM> configList)
    {
        return await OperationExecuteCustomAsync(async () =>
        {
            var result = await _systemConfigBLL.SaveAsync(configList);
            if (result.code == 1)
            {
                result.code = 1;
                result.msg = "操作成功";
            }
            else
            {
                result.code = -1;
                result.msg = result.msg;
            }
            return result;
        }, new AddOperationLogInput { Content = $"保存系统设置" });
    }
    #endregion
}
